import { ApiAdapter } from '@/utils/apiAdapter'
import type { IconNames, WeatherData, WeatherInfo } from '@/types/base'

export function  initGetWeatherInfo():Promise<{ latitude: number; longitude: number; weatherData: WeatherData }> {
  console.log('获取定位信息')
  console.log(navigator.geolocation)
  // 获取定位信息
  return new Promise((resolve, reject) => {
    if ('geolocation' in navigator) {
      navigator.geolocation.getCurrentPosition(position => {
        const { latitude, longitude } = position.coords
        // 获取天气信息
        const apiInstance = new ApiAdapter(latitude, longitude)
        apiInstance.init().then((res: unknown) => {
          console.log(res)
          resolve({
            latitude,
            longitude,
            weatherData: res as WeatherData,
          })
        }).catch(e=> {
          console.error(e)
          reject('未知异常')
        })
      },(e)=> {
        console.error('获取坐标失败',e)
        reject('获取坐标失败s')
      },{
        enableHighAccuracy : true
      })
    } else {
      console.error('浏览器不支持地理定位!')
      reject('浏览器不支持地理定位')
    }
  })

}

export function getWeatherIconByName(weather:string):IconNames{
  switch (weather) {
    case '多云':
      return 'icon_duoyun'
    case '晴':
      return 'icon_qingtian'
    case '阴':
      return 'icon_yintian'
    case '雨':
      return 'icon_xiaoyu'
    case '雪':
      return 'icon_xiaoxue'
    case '雾':
      return 'icon_wu'
    case '雷阵雨':
      return 'icon_leizhenyu'
    default:
      return 'icon_wushuju'
  }
}
